//
//  FinancingRZTableViewController.m
//  BigToBig
//
//  Created by 毛丽媛 on 16/7/15.
//  Copyright © 2016年 cn.cloudtp. All rights reserved.
//

#import "FinancingRZTableViewController.h"

@interface FinancingRZTableViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)NSArray *sectionArr;
@property(nonatomic,strong)NSArray *financingArr;
@property(nonatomic,strong)NSArray *detailArr;
@property(nonatomic,strong)NSMutableDictionary *dataDic;

@end

@implementation FinancingRZTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    UILabel *titleLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
    titleLab.text = @"融资需求";
    titleLab.textColor = [UIColor whiteColor];
    [self setBackBar];
    titleLab.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = titleLab;
    
    self.navigationController.navigationBar.translucent = NO;
    
    self.navigationController.navigationBar.barTintColor = COLOR_RGBA(77, 155, 255, 1);
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];
    self.tableView = tableView;
    self.tableView.bounces = NO;
    self.tableView.rowHeight = 70;
    self.tableView.showsVerticalScrollIndicator = NO;
    
    [self initDataSource];
    
}
#pragma mark 导航栏左右Button
- (void)setBackBar{
    
    BackButton *backBtn=[BackButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 0, 44, 44);
    [backBtn addTarget:self action:@selector(backController:) forControlEvents:UIControlEventTouchUpInside];
    [backBtn setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
    [backBtn.imageView setContentMode:UIViewContentModeLeft];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backBtn];
    [self.navigationItem setHidesBackButton:YES animated:YES];
    
    
    BackButton *saveBtn=[BackButton buttonWithType:UIButtonTypeCustom];
    saveBtn.frame = CGRectMake(kScreenWidth - 44,0 , 44, 44);
    [saveBtn addTarget:self action:@selector(SaveMessage) forControlEvents:UIControlEventTouchUpInside];
    [saveBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [saveBtn setTitle:@"保存" forState:UIControlStateNormal];
    [saveBtn.imageView setContentMode:UIViewContentModeLeft];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:saveBtn];
    [self.navigationItem setHidesBackButton:YES animated:YES];
    
}
#pragma mark 返回
- (void)backController:(UIButton *)button{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark 保存提交
- (void)SaveMessage{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"内容已完善" message:nil preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *ok = [UIAlertAction actionWithTitle:@"提交" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSArray *controllerArr =  self.navigationController.viewControllers;
        for (UIViewController *VC in controllerArr) {
            if ([NSStringFromClass([VC class]) isEqualToString:@"PersonCenterController"]) {
                [self.navigationController popToViewController:VC animated:YES];
            }
        }
    }];
    
    [alert addAction:ok];
    [self.navigationController
     presentViewController:alert animated:YES completion:nil];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return _sectionArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return _financingArr.count;
    }if (section == 1) {
        return _detailArr.count;
    }
    return 0;
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 50)];
    view.backgroundColor = [UIColor colorWithWhite:0.949 alpha:1.000];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, kScreenWidth, 50)];
    titleLabel.text = [self.sectionArr objectAtIndex:section];
    titleLabel.textColor = [UIColor grayColor];
    [view addSubview:titleLabel];
    
    return view;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 70)];
    view.backgroundColor = [UIColor colorWithWhite:0.949 alpha:1.000];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 0, kScreenWidth - 40, 70)];
    titleLabel.text = @"视频仅认证投资人可见,请先上传到优酷,视频帮助文档";
    titleLabel.font = [UIFont systemFontOfSize:14];
    titleLabel.numberOfLines = 0;
    titleLabel.textColor = [UIColor grayColor];
    
    [view addSubview:titleLabel];
    
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == 0) {
        return 0;
    }else {
        return 70;
    }
}

- (void)initDataSource{
    
    self.sectionArr = [[NSArray alloc] initWithObjects:@"融资需求",@"详情描述", nil];
    
    NSDictionary *dic00 = [[NSDictionary alloc] initWithObjectsAndKeys:@"融资总额",@"name",@"所需要的资金",@"content", nil];
    NSDictionary *dic01 = [[NSDictionary alloc] initWithObjectsAndKeys:@"出让股权",@"name",@"25%股权",@"content", nil];
    NSDictionary *dic02 = [[NSDictionary alloc] initWithObjectsAndKeys:@"众筹份数",@"name",@"??份",@"content", nil];
    NSDictionary *dic03 = [[NSDictionary alloc] initWithObjectsAndKeys:@"资金用途",@"name",@"简单介绍一下",@"content", nil];
    NSDictionary *dic04 = [[NSDictionary alloc] initWithObjectsAndKeys:@"资金外核核心需求",@"name",@"简单写一下需求",@"content", nil];
    self.financingArr = [[NSArray alloc] initWithObjects:dic00,dic01,dic02,dic03,dic04, nil];
    
    NSDictionary *dic10 = [[NSDictionary alloc] initWithObjectsAndKeys:@"项目视频",@"name",@" ",@"content", nil];
    self.detailArr = [[NSArray alloc] initWithObjects:dic10, nil];
    
    
    _dataDic = [[NSMutableDictionary alloc]initWithObjectsAndKeys:_financingArr,[_sectionArr objectAtIndex:0],_detailArr,[_sectionArr objectAtIndex:1],nil];
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *str = [_sectionArr objectAtIndex:indexPath.section];
    NSArray *arr = [_dataDic objectForKey:str];
    
    static NSString *CellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    cell = nil;
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    UILabel *userLabel=[[UILabel alloc]initWithFrame:CGRectMake(30, 0, kScreenWidth, 70)];
    userLabel.text=[[arr objectAtIndex:indexPath.row] valueForKey:@"name"];
    [cell.contentView addSubview:userLabel];
    
    UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(kScreenWidth / 2 - 20, 0, kScreenWidth / 2 - 10, 70)];
    contentLabel.text = [[arr objectAtIndex:indexPath.row] valueForKey:@"content"];
    contentLabel.textAlignment = NSTextAlignmentRight;
    contentLabel.font = [UIFont systemFontOfSize:15];
    contentLabel.textColor = [UIColor colorWithWhite:0.827 alpha:1.000];
    [cell.contentView addSubview:contentLabel];
    
    return cell;
    
}

#pragma mark 让分区标题随tableView滚动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    CGFloat sectionHeaderHeight = 50;
    
    if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
        
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
        
    } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
        
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
        
    }
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
